package com.codez.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.codez.domain.Menu;
import com.codez.service.MenuService;
import com.coedz.service.DUserService;

/**
 *  @author codez
 *	@createTime 2019-04-19 05:06:26
 */

@Controller
public class MenuController {

	@Autowired
	private MenuService menuService;
	
	@Reference
	private DUserService userService;
	
	@RequestMapping("list")
	public String list(Model model){
		List<Menu> findAll = menuService.findAll();
		model.addAttribute("menus", findAll);
		
		return "index";
	}
	
	@GetMapping("update/{id}")
	public String update(@PathVariable Integer id , Model model) {
		List<Menu> list = menuService.findAll();
		Menu menu = menuService.findById(id);
		model.addAttribute("menus", list);
		model.addAttribute("curr", menu);
		return "update";
	}
	
	
	@PostMapping("update")
	public String updateHandle(Menu menu) {
		int update = menuService.update(menu);
		if(update > 0) {
			return "redirect:/list";
		}else {
			return "redirect:/update/"+menu.getId();
		}
	}
	
	@GetMapping("hello")
	@ResponseBody
	public String hello(String name) {
		System.err.println(userService);
		return menuService.hello(name);
	}
	
}
